package com.bstek.bdf4.investigate.dao;


import com.bstek.bdf.core.view.DoradoJdbcDao;

import com.bstek.bdf4.investigate.entity.Category;
import com.bstek.dorado.data.provider.Page;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collection;


@Repository
public class CategoryDao extends DoradoJdbcDao {
    public void getAllCategories(Page<Category> page) {
        JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
        this.pagingQueryDorado(page, "select id, category_name, parent_id, detail from category where parent_id is null", new BeanPropertyRowMapper<>(Category.class) );
        Collection<Category> categoryList = page.getEntities();
        for (Category category : categoryList) {
            Category category1 = new Category();
            if (category != null && category.getParentId() != null) {
                category1 = jdbcTemplate.queryForObject("select id, category_name, detail from category where id = ?", new Object[]{category.getParentId()}, new BeanPropertyRowMapper<>(Category.class));
            } else {
                category1.setCategoryName("顶级类别");
            }
            category.setParent(category1);
        }
    }

}
